// 定义类
class Animal {
  // 类中有成员：成员属性和方法
  // 私有化属性，只能在类中使用
  #name = null
  // 构造方法
  constructor(name) {
    console.log('---------------constructor:Animal----------------')
    this.setName(name)
  }
  // 成员方法
  getName() {
    console.log('-----------getName:Animal------------------')
    return this.#name
  }
  setName(v) {
    this.#name = v
  }
}

// let a = new Animal('大象')
// console.log(a.getName())

// 声明Person类,继承Animal类
class Person extends Animal {
  constructor(name, age, id) {
    super(name) // 构造函数中的super(),一般是在构造函数中第一行,指向父类的构造函数
    console.log('---------------constructor:Person----------------')
    this.age = age
    this.id = id
  }
  // 和父类中同名的函数，成方法的重写，调用时，会执行就近原则
  getName() {
    console.log('------------------getName:Person-----------------')
    // 子类的函数中的super指向父类原型对象
    return '$' + super.getName()
  }
  showInfo() {
    console.log(`{id: ${this.id}, age: ${this.age}, name: ${this.getName()}}`)
  }
}

// let p = new Person('tom', 20, 1)
// p.showInfo()

// 书类
class Book {
  price = 110.5
  name = ''
  publish = '机械工业出版社'
  author = ''
  // 静态成员，是属于类的，不能使用对象调用，只能使用类名调用
  static PI = 3.14

  constructor(price, name, publish, author, st) {
    this.price = price
    this.name = name
    this.publish = publish
    this.author = author
    this.state = { st, list: [1, 2, 3] }
  }

  update() {
    this.setState({ st: false, list: [2, 3] })
  }

  setState(option) {
    Object.assign(this.state, option)
  }
}

let book = new Book(39.9, '水浒传', '中国文艺出版社', '施耐庵', true)
console.log(book)
book.update()
book.price = 49.9
console.log(book)
console.log(`PI = ${Book.PI}`)

let rt = Math.max.apply(null, [1, 20, 3])
console.log(rt)
